package Control;

import Model.DBUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.HashSet;

public class HistoryCode {

    //创建一个集合来存放历史记录
    public static HashSet<Car> historySet = new HashSet<>();

    @FXML
    private TableView<Car> tableHistoryCode;

    @FXML
    private TableColumn<Car, String> tableId;

    @FXML
    private TableColumn<Car, String> tableStayeTime;

    @FXML
    private TableColumn<Car, String> tableState;

    @FXML
    private Button btnUpPage1;

    @FXML
    private Label currentPage;

    @FXML
    private Button btnUpPage;

    @FXML
    private Button btnOut;

    @FXML
    void btnOut(ActionEvent event) {
        DBUtil.getHistoryData(historySet);
        this.showData();
    }

    @FXML
    void btnUpPage(ActionEvent event) {

    }

    public void showData(){
        ObservableList<Car> observable = FXCollections.observableArrayList(historySet);
        this.tableId.setCellValueFactory(new PropertyValueFactory<>("id"));
        this.tableStayeTime.setCellValueFactory(new PropertyValueFactory<>("timeout"));
        this.tableState.setCellValueFactory(new PropertyValueFactory<>("state"));
        this.tableHistoryCode.setItems(observable);
    }

    public static void showView() throws IOException {
        Stage HistoryStage = new Stage();
        Parent root = null;
        try {
            root = FXMLLoader.load(MyRunnable.class.getResource("HistoryCode.fxml"));
        }catch (Exception e){
            System.out.println("前端文件加载错误");
        }
        HistoryStage.setTitle("历史记录");
        HistoryStage.setScene(new Scene(root,430,525));
        HistoryStage.show();
    }
}
